package com.quectel.core.module.household.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.household.dao.HouseholdAuditDao;
import com.quectel.core.module.household.dto.HouseholdAuditDto;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.dto.HouseholdRoomDto;
import com.quectel.core.module.household.entity.HouseholdAuditEntity;
import com.quectel.core.module.household.service.HouseholdAuditService;
import com.quectel.core.module.household.service.HouseholdRoomService;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-12-14 09:31:59
 */
@DubboService
public class HouseholdAuditServiceImpl implements HouseholdAuditService {

    private static final Logger LOGGER = LoggerFactory.getLogger(HouseholdAuditServiceImpl.class);

    private static final String HOUSEHOLD_AUDIT_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "HOUSEHOLD_AUDIT_CACHE_BY_ID:";

    @Autowired
    private HouseholdAuditDao householdAuditDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private VillageRoomService villageRoomService;
    @Autowired
    private HouseholdService householdService;
    @Autowired
    private HouseholdRoomService householdRoomService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(HouseholdAuditDto householdAuditDto) {
        long currentTimeMillis = System.currentTimeMillis();

        HouseholdDto householdDto = householdService.selectByVillageIdAndMobile(householdAuditDto.getVillageId(), householdAuditDto.getMobile());
        if (householdDto == null) {
            // 该小区不存在该住户
            householdDto = CopyUtils.copyObj(householdAuditDto, HouseholdDto.class);
            householdDto.setType(HouseholdDto.TypeEnum.PERMANENT.getType());
            householdDto.setCreateTime(new Date(currentTimeMillis));
            Long save = householdService.save(householdDto);
            householdDto.setId(save);
        } else {
            HouseholdDto updately = new HouseholdDto();
            updately.setId(householdDto.getId());
            updately.setName(householdAuditDto.getName());
            updately.setFaceImg(householdAuditDto.getFaceImg());
            updately.setGender(householdAuditDto.getGender());
            householdService.updateById(updately);
        }

        HouseholdRoomDto householdRoomDto = householdRoomService.selectByHouseholdIdAndRoomId(householdDto.getId(), householdAuditDto.getRoomId());
        if (householdRoomDto == null) {
            // 绑定房屋关系
            householdRoomDto = new HouseholdRoomDto();
            householdRoomDto.setHouseholdId(householdDto.getId());
            householdRoomDto.setVillageId(householdAuditDto.getVillageId());
            householdRoomDto.setRoomId(householdAuditDto.getRoomId());
            householdRoomDto.setIdentityCategory(householdAuditDto.getIdentityCategory());
            householdRoomService.save(householdRoomDto);

        }
        // 审核条目更新
        HouseholdAuditDto newlyDto = new HouseholdAuditDto();
        newlyDto.setId(householdAuditDto.getId());
        newlyDto.setAuditStatus(householdAuditDto.getAuditStatus());
        newlyDto.setAuditOpinion(householdAuditDto.getAuditOpinion());
        newlyDto.setEditTime(new Date(currentTimeMillis));
        updateById(newlyDto);
    }

    @Override
    public HouseholdAuditDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                HOUSEHOLD_AUDIT_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public HouseholdAuditDto selectById(Long id) {
        HouseholdAuditEntity entity = householdAuditDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, HouseholdAuditDto.class));
    }

    @Override
    public List<HouseholdAuditDto> queryList(Map<String, Object> params) {
        List<HouseholdAuditEntity> list = householdAuditDao.queryList(params);
        List<HouseholdAuditDto> result = CopyUtils.copyList(list, HouseholdAuditDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private HouseholdAuditDto paddingField(HouseholdAuditDto dto) {
        if (dto != null) {
            dto.setVillage(villageService.selectById(dto.getVillageId()));
            dto.setVillageRoom(villageRoomService.selectById(dto.getRoomId()));
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return householdAuditDao.queryTotal(params);
    }

    @Override
    public Long save(HouseholdAuditDto dto) {
        HouseholdAuditEntity entity = CopyUtils.copyObj(dto, HouseholdAuditEntity.class);
        entity.setId(Snowflake.nextId());
        householdAuditDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(HouseholdAuditDto dto) {
        HouseholdAuditEntity entity = CopyUtils.copyObj(dto, HouseholdAuditEntity.class);
        householdAuditDao.updateById(entity);
        CacheKit.invalidRedisCache(HOUSEHOLD_AUDIT_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(HouseholdAuditDto dto) {
        HouseholdAuditEntity entity = CopyUtils.copyObj(dto, HouseholdAuditEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, householdAuditDao);

        CacheKit.invalidRedisCache(HOUSEHOLD_AUDIT_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        householdAuditDao.deleteById(id);

        CacheKit.invalidRedisCache(HOUSEHOLD_AUDIT_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
